<?php
namespace Admin\Controller;

class ChatController extends CommonController{
    
    public function index(){
        $this->assign('list',M('User')->where(array('hpt_id'=>1,'name'=>array('neq','')))->select());
        $this->display();
    }
    
    public function add(){
        //接收人信息
        $row = M('User')->find(I('get.id'));
        if(empty($row['pic'])) $row['pic'] = './Public/images/PersonalHeadImg.png';
        $this->assign('row',$row);
        
        //聊天记录
        $this->assign('list', $this->getRecord('system', I('get.phone')));
        $this->assign('sender', 'system');
        $this->display();
    }
    
    //获取更多聊天记录
    public function getMore(){
        $list = $this->getRecord('system', I('post.receiver'), I('post.p'));
        if(empty($list)) $list = 'err';
        $this->ajaxReturn($list);
    }
    
    /**
     * 获取聊天记录
     * @param string $sender
     * @param string $receiver
     * @param int $p
     * @return array 结构及
     */
    private function getRecord($sender, $receiver, $p = 1) {
        $where = "(sender='$sender' AND receiver='$receiver') or ( receiver='$sender' AND sender='$receiver' )";
        $list = M('Chat')->where($where)->order('id desc')->page($p, 10)->select();
        
        krsort($list);
        return $list;
    }
    
}